from fastapi import APIRouter

from ..schemas import SettingsRead, SettingsUpdate
from ..services.settings import read_settings, write_settings, cleanup_logs_if_needed

router = APIRouter()


@router.get("/", response_model=SettingsRead)
def get_settings_api() -> SettingsRead:
    cfg = read_settings()
    return SettingsRead(**cfg)  # type: ignore[arg-type]


@router.put("/", response_model=SettingsRead)
def update_settings_api(patch: SettingsUpdate) -> SettingsRead:
    cfg = write_settings({k: v for k, v in patch.model_dump(exclude_none=True).items()})
    # 可选：执行一次清理，避免长时间积累
    cleanup_logs_if_needed()
    return SettingsRead(**cfg)  # type: ignore[arg-type]

